package com.cykj.pos.service.impl;

import com.cykj.common.core.domain.AjaxResult;
import com.cykj.common.core.domain.entity.SysUser;
import com.cykj.common.utils.SecurityUtils;
import com.cykj.pos.domain.AizDeductionWallet;
import com.cykj.pos.domain.AizWithdrawalProfit;
import com.cykj.pos.domain.BizMerchant;
import com.cykj.pos.domain.dto.TransactionDetailsDTO;
import com.cykj.pos.domain.dto.TranscationListDTO;
import com.cykj.pos.mapper.BizMerchantMapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.StringUtils;
import com.cykj.pos.mapper.AizDeductionRecordsMapper;
import com.cykj.pos.domain.AizDeductionRecords;
import com.cykj.pos.service.IAizDeductionRecordsService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 未/未激活扣款记录Service业务层处理
 *
 * @author weijianbo
 * @date 2025-03-05
 */
@Service
public class AizDeductionRecordsServiceImpl extends ServiceImpl<AizDeductionRecordsMapper, AizDeductionRecords> implements IAizDeductionRecordsService {

    @Resource
    private BizMerchantMapper bizMerchantMapper;

    @Resource
    private AizDeductionRecordsMapper aizDeductionRecordsMapper;

    @Override
    public List<AizDeductionRecords> queryList(AizDeductionRecords aizDeductionRecords) {
//        LambdaQueryWrapper<AizDeductionRecords> lqw = Wrappers.lambdaQuery();
//        if (StringUtils.isNotBlank(aizDeductionRecords.getDeductionSn())){
//            lqw.eq(AizDeductionRecords::getDeductionSn ,aizDeductionRecords.getDeductionSn());
//        }
//        if (aizDeductionRecords.getDeductionMerchId() != null){
//            lqw.eq(AizDeductionRecords::getDeductionMerchId ,aizDeductionRecords.getDeductionMerchId());
//        }
//        if (StringUtils.isNotBlank(aizDeductionRecords.getDeductionMerchName())){
//            lqw.like(AizDeductionRecords::getDeductionMerchName ,aizDeductionRecords.getDeductionMerchName());
//        }
//        if (StringUtils.isNotBlank(aizDeductionRecords.getDeductionType())){
//            lqw.eq(AizDeductionRecords::getDeductionType ,aizDeductionRecords.getDeductionType());
//        }
//        if (aizDeductionRecords.getDeductionAmount() != null){
//            lqw.eq(AizDeductionRecords::getDeductionAmount ,aizDeductionRecords.getDeductionAmount());
//        }
//        if (StringUtils.isNotBlank(aizDeductionRecords.getDeductionResult())){
//            lqw.eq(AizDeductionRecords::getDeductionResult ,aizDeductionRecords.getDeductionResult());
//        }
//        if (StringUtils.isNotBlank(aizDeductionRecords.getDeductionStatus())){
//            lqw.eq(AizDeductionRecords::getDeductionStatus ,aizDeductionRecords.getDeductionStatus());
//        }
//        if (aizDeductionRecords.getDeductionLastAmount() != null){
//            lqw.eq(AizDeductionRecords::getDeductionLastAmount ,aizDeductionRecords.getDeductionLastAmount());
//        }
        if (aizDeductionRecords.getParams().get("beginTime") != null){
            aizDeductionRecords.setStartTime(aizDeductionRecords.getParams().get("beginTime")+" 00:00:00");
            aizDeductionRecords.setEndTime(aizDeductionRecords.getParams().get("endTime")+" 23:59:59");
        }
        SysUser sysUserman = SecurityUtils.getLoginUser().getUser();
        aizDeductionRecords.setDeptId(sysUserman.getDeptId());
        return aizDeductionRecordsMapper.queryList(aizDeductionRecords);
    }

//    @Override
//    public AjaxResult appBackDeductionRecords(AizDeductionRecords aizDeductionRecords) {
//        BizMerchant bizMerchant = bizMerchantMapper.selectAtmerchId(aizDeductionRecords.getUserId());
//        List<AizDeductionRecords> aizDeductionRecordsList = aizDeductionRecordsMapper.selectAppBackDeductionRecords(bizMerchant.getMerchId());
//        return AjaxResult.success("扣款列表", aizDeductionRecordsList);
//    }

    @Override
    public AjaxResult appBackDeductionRecords(TransactionDetailsDTO detailsDTO) {
        AizDeductionRecords aizDeductionRecords = new AizDeductionRecords();
        aizDeductionRecords.setPageNo((detailsDTO.getPageNo()-1)*detailsDTO.getPageSize());
        aizDeductionRecords.setPageSize(detailsDTO.getPageSize());
        aizDeductionRecords.setDeductionMerchId(detailsDTO.getMerchId());
        if(detailsDTO.getCupsName() != null){
            aizDeductionRecords.setDeductionMerchName(detailsDTO.getCupsName());
        }else if(detailsDTO.getPosSn() != null){
            aizDeductionRecords.setDeductionSn(detailsDTO.getPosSn());
        }
        aizDeductionRecords.setStartTime(detailsDTO.getStartTime());
        aizDeductionRecords.setEndTime(detailsDTO.getEndTime());
        List<TranscationListDTO> aizDeductionRecordsList =
                aizDeductionRecordsMapper.selectAppBackDeductionRecords(aizDeductionRecords);
        detailsDTO.setNumber(aizDeductionRecordsList.size());
        detailsDTO.setProfitSharingAmount(aizDeductionRecordsList.stream().mapToDouble(TranscationListDTO::getProfitSharingAmount).sum());
        detailsDTO.setTransactionAmount(aizDeductionRecordsList.stream().mapToDouble(TranscationListDTO::getTransactionAmount).sum());
        detailsDTO.setDtolist(aizDeductionRecordsList);
        return AjaxResult.success("扣款记录",detailsDTO);
    }

    @Override
    public AizDeductionWallet getWalletByUserId(Long userId) {
        return aizDeductionRecordsMapper.getWalletByUserId(userId);
    }

    @Override
    public void insertWalletData(AizDeductionWallet aizDeductionWallet_new) {
        aizDeductionRecordsMapper.insertWalletData(aizDeductionWallet_new);
    }

    @Override
    public void updateWalletData(AizDeductionWallet aizDeductionWallet) {
        aizDeductionRecordsMapper.updateWalletData(aizDeductionWallet);
    }
}
